<script>
    var __CategoryAddUrl   = '/cms/category/user_add.mhtml?parentId=';
    var __CategoryEditUrl   = '/cms/category/user_edit.mhtml?resId=';
    var __CategoryDelUrl    = '/cms/category/user_del.do';
    var __UserCategoryList = '/cms/category/user_list.do';
    var __DetailAddUrl   = '/cms/detail/add.mhtml';
    var __DetailEditUrl   = '/cms/detail/edit.mhtml?resId=';
    var __DetailDelUrl    = '/cms/detail/del.do';
    var __DetailListUrl  = '/cms/detail/list.do';
    

    let EleTree ;
    function openAddCategoryPanel(pid){
        let index = layer.open({
            title: '添加子栏目',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __CategoryAddUrl+pid,
        });
    }
    function openEditCategoryPanel(resId){
        let index = layer.open({
            title: '编辑栏目信息',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __CategoryEditUrl+resId,
        });
    }
    function delCategoryData (resId){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: __CategoryDelUrl,
            data: {resId:resId},
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        window.location.reload()
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            }
        });
    }
    function delDetailData (resId){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: __DetailDelUrl,
            data: {resId:resId},
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        window.location.reload()
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            }
        });
    }
    function listDetailRender(handler){

        handler.render({
            elem: '#currentTableId',
            url: __DetailListUrl,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'title', minWidth: 160, title: '标题'},
                {field: 'codeName', width: 160, title: '发布人'},
                {field: 'orderNum', width: 80, title: '状态', sort: true},
                {field: 'bak', width:100 , title: '发布时间'},

                {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done:function(res, curr, count){
                console.log(res)
            }
        });
    }
    function initSiteData (eleTree,data){

        let obj={
            elem: '.eleTree',
            data: data,
            showLine:true,
            // url: "../eleTree/tree.json",
            renderAfterExpand: true,
            expandOnClickNode: false,
            contextmenuList:[{eventName: "editDept", text: "编辑栏目"},{eventName: "addChildDept", text: "添加子栏目"},{eventName: "delDept", text: "删除栏目"}]
        }
        $("#btn-add").click(function(e){
            e.preventDefault();
            openAddCategoryPanel(-1);
        });
        //添加子部门
        eleTree.on("nodeAddChildDept",function(d) {
            openAddCategoryPanel(d.data.id);
        })
        //删除部门
        eleTree.on("nodeDelDept",function(d) {

            layer.confirm("确认删除该记录？",{
                title:"操作确认",
                yes:function(index,layero){
                    delCategoryData(d.data.id)
                    layer.close(index)
                },
                cancel:function(index){
                }

            })
        })
        //编辑部门信息
        eleTree.on("nodeEditDept",function(d) {
            openEditCategoryPanel(d.data.id)
        })
        EleTree = eleTree.render(obj);
    }
    layui.use(['form', 'table','eleTree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        let eleTree = layui.eleTree;

        $.ajax({
            type: "get",
            url: __UserCategoryList,
            success: function (res) {
                let treeData = listToTree("id","parentId",res.data)
                if(res.code==0){
                    initSiteData(eleTree,treeData)
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            },
            complete:function (){
                layer.closeAll();
            }
        });

        listDetailRender(table)
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: __DetailAddUrl,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: __DetailEditUrl +data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {

                layer.confirm("确认删除该记录？",{
                    title:"操作确认",
                    yes:function(index,layero){
                        delData(data.id)
                        layer.close(index)
                    },
                    cancel:function(index){
                    }

                })
            }
        });
        //el1.append(d,newData)

    });

</script>
